<?php
class control_gift extends core_action{
	
	public $params;
	
	function __construct(){
		$this->params['gift'] = 'tabOn';
	}
	
	function index(){
		$gift = new core_model_gift();
		$this->params['title'] = "礼品兑换";
		$this->params['rs'] = $gift->select("stock>0","*","","order by rank desc")->items;
		$this->render('default/gift_index.php', $this->params);
	}
	
	function exchange(){
		if($this->islogin){
			$this->showMsg('请先登录', 2, Qtpl::createUrl('user', 'login'));
		}
		$gid = intval(Qutil::enId($_GET['gid']));
		if($_GET['taken'] != md5($_COOKIE['skey'].core_lib_constant::COOKIE_KEY)){
			$this->showMsg('参数错误', 2);
		}
		$gift = new core_model_gift($gid);
		if($gift->getData('stock') <= 0){
			$this->showMsg('库存不足或者商品不存在', 2);
		}
		$srvUser = new core_service_user($_COOKIE['uid']);
		$consignee['zip'] = $srvUser->get('zip');
		$consignee['phone'] = $srvUser->get('phone');
		$consignee['address'] = $srvUser->get('address');
		if(empty($consignee['phone']) or empty($consignee['address'])){
			$this->showMsg('收货信息不完善，请先完善。', 2,Qtpl::createUrl('user', 'edit'));
		}
		if($srvUser->exchange($gid, $consignee) === false){
			$this->showMsg('兑换出错：'.$srvUser->getError(), 2);
		}
		$this->redirict(Qtpl::createUrl('user', 'order'));
	}
}